package com.shouren;

import com.shouren.args.ListInt;
import com.shouren.args.Location;
import com.shouren.args.Port;
import com.shouren.args.type.DoubleArgs;
import com.shouren.args.type.ListIntegerArgs;
import com.shouren.args.type.LongArgs;
import com.shouren.args.type.StringArgs;
import com.sun.deploy.util.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {

    private Location location;
    private Port port;
    private ListInt listInt;

    public void parse(String arguments) {

        location = new Location(getValueByFlag(arguments,Location.flag));
        port = new Port(getValueByFlag(arguments,Port.flag));
        listInt = new ListInt(getValueByFlag(arguments,ListInt.flag));
    }

    public String getValueByFlag(String arguments, String flag) {
        if(!arguments.contains("-" + flag)){
            return null;
        }
        int i = arguments.indexOf("-" + flag);
        String substring = arguments.substring(i+2);
        if(substring == null || substring.trim().equals("")){
            return null;
        }
        Pattern pt = Pattern.compile("-[a-z]");
        Matcher matcher = pt.matcher(substring);
        if (matcher.find()){
            int start = matcher.start();
           return substring.substring(0,start).trim();
        }

        return substring.trim();
    }


    public String getLocation(){
       return (String) location.baseArgs().getData();
    }

    public Long getPort(){
        return (Long) port.baseArgs().getData();
    }

    public List<Integer> getListInt(){
        return (List<Integer>) listInt.baseArgs().getData();
    }


}

